﻿<%@ WebHandler Language="C#" Class="PanasonicWarranty.UI.UpLoad5" %>

using System;
using System.Web;
using System.IO;
using PanasonicWarranty.Business;

namespace PanasonicWarranty.UI
{
    /// <summary>
    /// Summary description for UpLoad
    /// </summary>
    public class UpLoad5 : BaseHandle
    {
        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;
            string filename = "";
            string savepath = "";
            string filepath = "";
            try
            {
                Authorize(Role.Upload);
                HttpPostedFile postedFile = context.Request.Files["Filedata"];
                string tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
                savepath = context.Server.MapPath(tempPath);
                filename = Guid.NewGuid() + postedFile.FileName;
                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);
                postedFile.SaveAs(savepath + @"\" + filename);

                //Insert dữ liệu
                filepath = Path.Combine(savepath, filename);
                int rowaffected = pa_Agency.ImportExcel(filepath);

                //Ghi log tương tác dữ liệu
                SysChangeLog.Insert(UserID.Value, "Import danh sách đại lý");
                context.Response.Write("Upload thành công " + rowaffected + " dữ liệu");
                context.Response.StatusCode = 200;
            }
            catch (Exception e)
            {
                SysLog.InsertLog(e);
                context.Response.Write(e.Message);
                context.Response.StatusCode = 200;
            }
            finally
            {
                //Sau khi insert dữ liệu vào db, xóa file đã upload
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
            }
        }
    }
}
